#!/usr/bin/env bash

set -e

HOME_DIR=$(eval echo "~")
WORKING_DIR="${HOME_DIR}/.deep-speaker-wd"

if [ $# -lt 1 ]; then
  echo "Usage : $0 Task [download_librispeech, build_mfcc, build_model_inputs, train_softmax, train_triplet]"
  exit
fi

PRE_TRAINING_WORKING_DIR="${WORKING_DIR}/pre-training"
TRIPLET_TRAINING_WORKING_DIR="${WORKING_DIR}/triplet-training"

mkdir -p "${WORKING_DIR}"

case "$1" in

download_librispeech)
  echo "[download_librispeech] selected."
  # WORKING_DIR/LibriSpeech
  cp download_librispeech.sh "${WORKING_DIR}"
  cd "${WORKING_DIR}" && bash download_librispeech.sh && cd -
  ;;

build_mfcc)
  echo "[build_mfcc] selected."
  python cli.py build-mfcc-cache --working_dir "${PRE_TRAINING_WORKING_DIR}" --audio_dir "${WORKING_DIR}/LibriSpeech/train-clean-360"
  python cli.py build-mfcc-cache --working_dir "${TRIPLET_TRAINING_WORKING_DIR}" --audio_dir "${WORKING_DIR}/LibriSpeech"
  ;;

build_model_inputs)
  echo "[build_model_inputs] selected."
  python cli.py build-keras-inputs --working_dir "${PRE_TRAINING_WORKING_DIR}"
  ;;

train_softmax)
  # Pre-training (0.92k speakers).
  echo "[train_softmax] selected."
  python cli.py train-model --working_dir "${PRE_TRAINING_WORKING_DIR}" --pre_training_phase
  ;;

train_triplet)
  # Triplet-training (2.48k speakers).
  echo "[train_triplet] selected."
  python cli.py train-model --working_dir "${TRIPLET_TRAINING_WORKING_DIR}"
  ;;

*)
  echo "Unknown option."
  ;;

esac
